package com.liangsui.mss.controller.erp;

import com.liangsui.mss.aspectj.Log;
import com.liangsui.mss.aspectj.enums.BusinessType;
import com.liangsui.mss.constants.Constants;
import com.liangsui.mss.domain.Purchase;
import com.liangsui.mss.domain.PurchaseItem;
import com.liangsui.mss.dto.PurchaseDto;
import com.liangsui.mss.dto.PurchaseFormDto;
import com.liangsui.mss.service.PurchaseService;
import com.liangsui.mss.utils.IdGeneratorSnowflake;
import com.liangsui.mss.utils.ShiroSecurityUtils;
import com.liangsui.mss.vo.AjaxResult;
import com.liangsui.mss.vo.DataGridView;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PurchaseController
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/12/6 14:36
 */
@RestController
@RequestMapping({"erp/purchase"})
public class PurchaseController {

    @DubboReference
    private PurchaseService purchaseService;

    public PurchaseController() {
    }

    @GetMapping({"listPurchaseForPage"})
    public AjaxResult listPurchaseForPage(PurchaseDto purchaseDto) {
        DataGridView gridView = this.purchaseService.listPurchasePage(purchaseDto);
        return AjaxResult.success("查询成功", gridView.getData(), gridView.getTotal());
    }

    @GetMapping({"listPurchasePendingForPage"})
    public AjaxResult listPurchasePendingForPage(PurchaseDto purchaseDto) {
        purchaseDto.setStatus(Constants.STOCK_PURCHASE_STATUS_2);
        DataGridView gridView = this.purchaseService.listPurchasePage(purchaseDto);
        return AjaxResult.success("查询成功", gridView.getData(), gridView.getTotal());
    }

    @PostMapping({"doAudit/{purchaseId}"})
    @Log(title = "采购单管理--提交审核", businessType = BusinessType.UPDATE)
    public AjaxResult doAudit(@PathVariable String purchaseId){
        //根据purchaseId查询单据对象
        Purchase purchase=this.purchaseService.getPurchaseById(purchaseId);
        if(purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_1)
                || purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_4)){
            int i = this.purchaseService.doAudit(purchaseId, ShiroSecurityUtils.getCurrentSimpleUser());
            return AjaxResult.toAjax(i);
        }else{
            return AjaxResult.fail("当前单据状态不是【未提交】或【审核失败】状态，不能提交审核");
        }
    }

    @PostMapping({"doInvalid/{purchaseId}"})
    @Log(title = "采购单管理--作废", businessType = BusinessType.UPDATE)
    public AjaxResult doInvalid(@PathVariable String purchaseId){
        //根据purchaseId查询单据对象
        Purchase purchase=this.purchaseService.getPurchaseById(purchaseId);
        if(purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_1)
                || purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_4)){
            int i = this.purchaseService.doInvalid(purchaseId);
            return AjaxResult.toAjax(i);
        }else{
            return AjaxResult.fail("当前单据状态不是【未提交】或【审核失败】状态，不能作废");
        }
    }

    @PostMapping({"auditPass/{purchaseId}"})
    @Log(title = "采购单管理--审核通过", businessType = BusinessType.UPDATE)
    public AjaxResult auditPass(@PathVariable String purchaseId){
        //根据purchaseId查询单据对象
        Purchase purchase=this.purchaseService.getPurchaseById(purchaseId);
        if(purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_2)) {
            int i = this.purchaseService.auditPass(purchaseId);
            return AjaxResult.toAjax(i);
        } else {
            return AjaxResult.fail("当前单据状态不是【待审核】状态，不能审核");
        }
    }

    @PostMapping({"auditNoPass/{purchaseId}/{auditMsg}"})
    @Log(title = "采购单管理--审核不通过", businessType = BusinessType.UPDATE)
    public AjaxResult auditNoPass(@PathVariable String purchaseId, @PathVariable String auditMsg){
        //根据purchaseId查询单据对象
        Purchase purchase=this.purchaseService.getPurchaseById(purchaseId);
        if(purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_2)){
            int i = this.purchaseService.auditNoPass(purchaseId, auditMsg);
            return AjaxResult.toAjax(i);
        }else{
            return AjaxResult.fail("当前单据状态不是【待审核】状态，不能审核");
        }
    }

    @GetMapping({"getPurchaseItemById/{purchaseId}"})
    public AjaxResult getPurchaseItemById(@PathVariable String purchaseId) {
        List<PurchaseItem> list=this.purchaseService.getPurchaseItemById(purchaseId);
        return AjaxResult.success(list);
    }

    /**
     * 生成单据号
     */
    @GetMapping("generatePurchaseId")
    public AjaxResult generatePurchaseId() {
        return AjaxResult.success(IdGeneratorSnowflake.generatorIdWithProfix(Constants.ID_PREFIX_CG));
    }

    /**
     * 暂存采购单数据和详情
     */
    @PostMapping("addPurchase")
    @Log(title = "采购单管理--暂存采购单位和详情数据", businessType = BusinessType.INSERT)
    public AjaxResult addPurchase(@RequestBody PurchaseFormDto purchaseFormDto) {
        //判断当前采购单的状态
        if (!checkPurchase(purchaseFormDto)) {
            return AjaxResult.fail("当前单据状态不是【未提交】或【审核失败】状态，不能进行修改");
        }
        purchaseFormDto.getPurchaseDto().setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.purchaseService.addPurchaseAndItem(purchaseFormDto));

    }

    /**
     * 保存并提交审核采购单数据和详情
     */
    @PostMapping("addPurchaseToAudit")
    @Log(title = "采购单管理--添加并提交审核采购单位和详情数据", businessType = BusinessType.INSERT)
    public AjaxResult addPurchaseToAudit(@RequestBody PurchaseFormDto purchaseFormDto) {
        //判断当前采购单的状态
        if (!checkPurchase(purchaseFormDto)) {
            return AjaxResult.fail("当前单据状态不是【未提交】或【审核失败】状态，不能进行修改");
        }
        purchaseFormDto.getPurchaseDto().setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.purchaseService.addPurchaseAndItemToAudit(purchaseFormDto));

    }

    /**
     * 公共验证采购单的方法
     */
    private boolean checkPurchase(PurchaseFormDto purchaseFormDto) {
        String purchaseId = purchaseFormDto.getPurchaseDto().getPurchaseId();
        Purchase purchase = this.purchaseService.getPurchaseById(purchaseId);
        //判断ID在数据库里面是否存在，如果存在，说明是再次暂存
        if (null == purchase) {
            return true;
        }
        if (purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_1)
                || purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_4)) {
            return true;
        }
        return false;
    }

    /**
     * 根据采购单号查询采购单信息和详情信息
     */
    @GetMapping("queryPurchaseAndItemByPurchaseId/{purchaseId}")
    public AjaxResult queryPurchaseAndItemByPurchaseId(@PathVariable String purchaseId) {
        Purchase purchase = this.purchaseService.getPurchaseById(purchaseId);
        if (null == purchase) {
            return AjaxResult.fail("单据号【" + purchaseId + "】不存在");
        } else {
            //查询详情
            List<PurchaseItem> items = this.purchaseService.getPurchaseItemById(purchaseId);
            Map<String, Object> res = new HashMap<>();
            res.put("purchase", purchase);
            res.put("items", items);
            return AjaxResult.success(res);
        }
    }

    /**
     * 入库
     */
    @PostMapping("doInventory/{purchaseId}")
    @Log(title = "采购单管理--入库", businessType = BusinessType.UPDATE)
    public AjaxResult doInventory(@PathVariable String purchaseId) {
        Purchase purchase = this.purchaseService.getPurchaseById(purchaseId);
        if (purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_3)) {
            //进行入库
            return AjaxResult.toAjax(this.purchaseService.doInventory(purchaseId, ShiroSecurityUtils.getCurrentSimpleUser()));
        } else if (purchase.getStatus().equals(Constants.STOCK_PURCHASE_STATUS_6)) {
            return AjaxResult.fail("采购单【" + purchaseId + "】已入库，不能重复入库");
        } else {
            return AjaxResult.fail("采购单【" + purchaseId + "】没有审核通过，不能入库");
        }
    }
}
